package com.fh.service.impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/11/2511:37
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fh.common.TableData;
import com.fh.entity.po.*;
import com.fh.entity.vo.KeyVo;
import com.fh.entity.vo.ShopVo;
import com.fh.mappers.*;
import com.fh.service.ShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * created by macro on 2021/11/25,
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private KeyMapper keyMapper;
    @Resource
    private ValueMapper valueMapper;
    @Resource
    private ShopValuesMapper shopValuesMapper;
    @Resource
    private ImgsMapper imgsMapper;
    @Override
    public Shop queryById(Integer id) {
        return shopMapper.queryById(id);
    }

    @Override
    public void upShop(Shop shop) {
        shopMapper.upShop(shop);
    }

    @Override
    public void addShop(Shop shop) {
        shop.setCreateDate(new Date());
        shop.setIsdel(0);
        shopMapper.addShop(shop);
    }

    @Override
    public Map queryIsSkuData(Integer id) {
        Map map =new HashMap();
        List<KeyVo> skuData=new ArrayList<>();
        List<KeyVo> keyData=new ArrayList<>();
        List<KeyVo> types = keyMapper.queryAllData(id);

        for (int i = 0; i < types.size(); i++) {
            KeyVo typeVo = types.get(i);
            // 根据属性id 查询值
            List<Value> values = valueMapper.queryByIdValue(typeVo.getId());
            if (typeVo.getoptionType()==3){
                typeVo.setVals(new ArrayList<>());
            }else {
                typeVo.setVals("");
            }
            typeVo.setValues(values);
            if (typeVo.getIsSku()==0){
                keyData.add(typeVo);
            }else {
                skuData.add(typeVo);
            }

        }

        map.put("skuData",skuData);
        map.put("keyData",keyData);
        return map;
    }

    @Override
    public void addShops(Shop shop) {
        shop.setCreateDate(new Date());
        shop.setIsdel(0);
        shopMapper.addShops(shop);

        System.out.println(shop.getId());
        JSONArray jsonArray = JSONArray.parseArray(shop.getValuess());
        List<ShopValues> v=new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject o = (JSONObject) jsonArray.get(i);
            ShopValues shopValues = new ShopValues();
            shopValues.setValuess(o.toJSONString());
            shopValues.setPid(shop.getId());
            v.add(shopValues);
        }
        shopValuesMapper.addShopValues(v);

        JSONArray objects = JSONArray.parseArray(shop.getImgs());
        List<Imgs> i=new ArrayList<>();
        for (int j = 0; j < objects.size(); j++) {
            JSONObject o2 = (JSONObject) objects.get(j);
            Imgs imgs = new Imgs();
            imgs.setPid(shop.getId());
            imgs.setName(o2.getString("name"));
            imgs.setImg(o2.getString("url"));
            i.add(imgs);
        }
        imgsMapper.addImgs(i);
    }

    @Override
    public TableData<Shop> queryPageData(ShopVo shopVo) {

        return new TableData<>(shopMapper.queryCount(shopVo),shopMapper.queryShopVo(shopVo));
    }

    @Override
    public List<Imgs> queryImgsById(Integer id) {
        return shopMapper.queryImgsById(id);
    }

    @Override
    public void addShopImgs(Integer pid, String imgsList) {
        JSONArray objects = JSONArray.parseArray(imgsList);
        List<Imgs> i=new ArrayList<>();
        for (int j = 0; j < objects.size(); j++) {
            JSONObject o2 = (JSONObject) objects.get(j);
            Imgs imgs = new Imgs();
            imgs.setPid(pid);
            imgs.setName(o2.getString("name"));
            imgs.setImg(o2.getString("url"));
            i.add(imgs);
        }
        imgsMapper.addImgs(i);
    }

    @Override
    public void delImgs(Integer id) {
        imgsMapper.delImgs(id);
    }

    @Override
    public List<ShopValues> queryShopValuesById(Integer id) {
        return shopMapper.queryShopValuesById(id);
    }

    @Override
    public Map queryShopValuesByPid(Integer id) {
        Map rs=new HashMap(); //skudata attrdata
        JSONObject attrData=null;
        List<JSONObject> skuData=new ArrayList<>();
        //查询所有的数据
        List<String> shopValues = shopMapper.queryValuesByPid(id);
        for (int i = 0; i <shopValues.size() ; i++) {
            JSONObject parse = (JSONObject) JSONObject.parse(shopValues.get(i));
            Object count = parse.get("count");
            if(count!=null){
                skuData.add(parse);
            }else{
                attrData=parse;
            }
        }

        rs.put("sd",skuData);
        rs.put("ad",attrData);

        return rs;
    }

    @Override
    public void deleteShopValues(Integer pid) {
        shopValuesMapper.deleteShopValues(pid);
    }

    @Override
    public void upValues(Integer pid, String valuess) {
        JSONArray jsonArray = JSONArray.parseArray(valuess);
        List<ShopValues> v=new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject o = (JSONObject) jsonArray.get(i);
            ShopValues shopValues = new ShopValues();
            shopValues.setValuess(o.toJSONString());
            shopValues.setPid(pid);
            v.add(shopValues);
        }
        shopValuesMapper.addShopValues(v);
    }
}
